﻿#Region "Imports"

Imports MS.WindowsAPICodePack.Internal.CoreHelpers

Imports Microsoft.WindowsAPICodePack
Imports Microsoft.WindowsAPICodePack.Dialogs

#End Region

Class MainWindow

#Region "MainWindow"

    Private Sub MainWindow_Closing(ByVal sender As Object, ByVal e As System.ComponentModel.CancelEventArgs) Handles Me.Closing
        If Me.WindowState = Windows.WindowState.Maximized Then
            My.Settings.MainWindow_IsMax = True
        Else
            My.Settings.MainWindow_IsMax = False
            My.Settings.MainWindow_Size = New Size(Me.Width, Me.Height)
        End If
        My.Settings.Save()
    End Sub

    Private Sub MainWindow_Loaded(ByVal sender As Object, ByVal e As System.Windows.RoutedEventArgs) Handles Me.Loaded
        If RunningOnVista Or RunningOnWin7 Then
            Me.IsGlassEnabled = True
            GlassHelper.ExtendGlassFrame(Me, New Thickness(-1, -1, -1, -1))
        End If

        If My.Settings.MainWindow_IsMax Then
            Me.WindowState = Windows.WindowState.Maximized
        Else
            Me.Width = My.Settings.MainWindow_Size.Width
            Me.Height = My.Settings.MainWindow_Size.Height
        End If

        If My.Application.StartUpFileNames.Count > 0 Then
            Dim f As New IO.FileInfo(My.Application.StartUpFileNames.Item(0))
            Dim fs As IO.FileStream = IO.File.OpenRead(f.FullName)
            Dim tr As New TextRange(DocumentView.Document.ContentStart, DocumentView.Document.ContentEnd)
            If f.Extension = ".xaml" Then
                Dim xamlFile As New IO.FileStream(f.FullName, IO.FileMode.Open, IO.FileAccess.Read)
                Dim content As FlowDocument = TryCast(Markup.XamlReader.Load(xamlFile), FlowDocument)
                DocumentView.Document = content
                xamlFile.Close()
            ElseIf f.Extension = ".rtf" Then
                tr.Load(fs, Windows.DataFormats.Rtf)
            Else
                tr.Load(fs, Windows.DataFormats.Text)
            End If
            fs.Close()
            PrintMenuItem.IsEnabled = True
        End If
    End Sub

#End Region

#Region "MainMenu"

    Private Sub OpenButton_Click(ByVal sender As System.Object, ByVal e As System.Windows.RoutedEventArgs) Handles OpenMenuItem.Click
        If RunningOnVista Or RunningOnWin7 Then
            Dim open As New CommonOpenFileDialog
            If open.ShowDialog = CommonFileDialogResult.OK Then
                Dim f As New IO.FileInfo(open.FileName)
                Dim fs As IO.FileStream = IO.File.OpenRead(f.FullName)
                Dim tr As New TextRange(DocumentView.Document.ContentStart, DocumentView.Document.ContentEnd)
                If f.Extension = ".xaml" Then
                    Dim xamlFile As New IO.FileStream(f.FullName, IO.FileMode.Open, IO.FileAccess.Read)
                    Dim content As FlowDocument = TryCast(Markup.XamlReader.Load(xamlFile), FlowDocument)
                    DocumentView.Document = content
                    xamlFile.Close()
                ElseIf f.Extension = ".rtf" Then
                    tr.Load(fs, Windows.DataFormats.Rtf)
                Else
                    tr.Load(fs, Windows.DataFormats.Text)
                End If
                fs.Close()
                PrintMenuItem.IsEnabled = True
            End If
        Else 'Windows XP
            Dim open As New Microsoft.Win32.OpenFileDialog
            If open.ShowDialog = True Then
                Dim f As New IO.FileInfo(open.FileName)
                Dim fs As IO.FileStream = IO.File.OpenRead(f.FullName)
                Dim tr As New TextRange(DocumentView.Document.ContentStart, DocumentView.Document.ContentEnd)
                If f.Extension = ".xaml" Then
                    Dim xamlFile As New IO.FileStream(f.FullName, IO.FileMode.Open, IO.FileAccess.Read)
                    Dim content As FlowDocument = TryCast(Markup.XamlReader.Load(xamlFile), FlowDocument)
                    DocumentView.Document = content
                    xamlFile.Close()
                ElseIf f.Extension = ".rtf" Then
                    tr.Load(fs, Windows.DataFormats.Rtf)
                Else
                    tr.Load(fs, Windows.DataFormats.Text)
                End If
                fs.Close()
                PrintMenuItem.IsEnabled = True
            End If
        End If
    End Sub

    Private Sub PrintButton_Click(ByVal sender As Object, ByVal e As System.Windows.RoutedEventArgs) Handles PrintMenuItem.Click
        Dim pd As New PrintDialog
        If pd.ShowDialog = True Then
            pd.PrintVisual(DocumentView, "test")
        End If
    End Sub

    Private Sub AboutButton_Click(ByVal sender As Object, ByVal e As System.Windows.RoutedEventArgs) Handles AboutMenuIem.Click
        Dim a As New AboutDialog
        a.ShowDialog()
    End Sub

    Private Sub ExitMenuItem_Click(ByVal sender As Object, ByVal e As System.Windows.RoutedEventArgs) Handles ExitMenuItem.Click
        Close()
    End Sub

#End Region

End Class